#!/opt/python-2.5/bin/python2.5

bgcolor = "333366"
text = "EEEEEE"
font = "Arial"
linkc = "9999FF"
vlinkc = "CC66CC"

import cgi, cgitb, os, sys #, mimetools, subprocess, shutil
cgitb.enable()
print "Content-type: text/html;charset=utf-8"
print 
print "<body bgcolor=%s text=%s link=%s vlink=%s>" % (bgcolor, text, linkc, vlinkc)
ULForm = cgi.FieldStorage()
ULFile = "tempdir/uploads.file"
if ("File" in ULForm.keys() and "Description" in ULForm.keys()):
	desc = ULForm["Description"].value
	evidence = ULForm["File"]
	if evidence.file:
		file = open(ULFile, 'a+')
		max = 0
		for line in file:
			num = int(line.split("\t")[0])
			if num > max:	max = num
		imagenum = str(max + 1)
		file.write(imagenum + "\t" + desc + "\n")
		file.close()
		data = evidence.file.read()
		file = open("uploaded/" + imagenum, 'w')
		file.write(data)
		file.close()
print "<font face=%s size=4><b>Graphic evidence\n<br/></b></font>" % font
print "<form name=\"Upload Form\" method=post enctype=\"multipart/form-data\" action=upload.cgi>"
print "<font face=%s size=3>" % font
print "Title of supporting evidence:  <input name=\"Description\" type=text size=30 maxlength=255 autocomplete/></br>"
print "Support file:  "
print "<input type=file name=\"File\" size=30/></br>\t"
print "<button type=submit name=\"Submit\">Submit evidence</button></br>"
print "</form></font>"
print "<font face=%s size=4><i>Submitted so far:\n<br/></i></font>" % font
print "<font face=%s size=3>" % font
try:
	file = open(ULFile, 'r')
	lines = 0
	for line in file:
		lines += 1
		filename = line.split("\t")[0]
		descriptor = "\t".join(line.split("\t")[1:])
		print "<a href=uploaded/%s />%s</a></br>" % (filename, descriptor)
	file.close()
	if lines == 0:	raise IOError
except (IOError):
	print "Nothing so far.  Be the first to submit pictoral support!"
print "</font>"
print "</body>"
